package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SprTable {
  private static final Log _log = LogFactory.getLog(SprTable.class);
  
  private static final Map<Integer, Spr> _dataMap = new HashMap<>();
  
  private static SprTable _instance;
  
  public static SprTable get() {
    if (_instance == null)
      _instance = new SprTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    Spr spr = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `spr_action`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int key = rs.getInt("spr_id");
        if (!_dataMap.containsKey(Integer.valueOf(key))) {
          spr = new Spr();
          _dataMap.put(Integer.valueOf(key), spr);
        } else {
          spr = _dataMap.get(Integer.valueOf(key));
        } 
        int actid = rs.getInt("act_id");
        int frameCount = rs.getInt("framecount");
        if (frameCount < 0)
          frameCount = 0; 
        int frameRate = rs.getInt("framerate");
        if (frameRate < 0)
          frameRate = 0; 
        int speed = calcActionSpeed(frameCount, frameRate);
        switch (actid) {
          case 0:
          case 4:
          case 11:
          case 20:
          case 24:
          case 40:
          case 46:
          case 50:
          case 54:
          case 58:
          case 62:
            spr._moveSpeed.put(Integer.valueOf(actid), Integer.valueOf(speed));
            break;
          case 2:
            spr._dmg = speed;
            break;
          case 18:
            spr._dirSpellSpeed = speed;
            break;
          case 19:
            spr._nodirSpellSpeed = speed;
            break;
          case 30:
            spr._dirSpellSpeed30 = speed;
            break;
          case 1:
          case 5:
          case 12:
          case 21:
          case 25:
          case 41:
          case 47:
          case 51:
          case 55:
          case 59:
          case 63:
            spr._attackSpeed.put(Integer.valueOf(actid), Integer.valueOf(speed));
            break;
        } 
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入图形影格资料数量: " + _dataMap.size() + "(" + timer.get() + "ms)");
  }
  
  private int calcActionSpeed(int frameCount, int frameRate) {
    return (int)((frameCount * 40) * 24.0D / frameRate);
  }
  
  public int getAttackSpeed(int sprid, int actid) {
    if (!_dataMap.containsKey(Integer.valueOf(sprid)))
      return 0; 
    if ((_dataMap.get(Integer.valueOf(sprid)))._attackSpeed.containsKey(Integer.valueOf(actid)))
      return ((Integer)(_dataMap.get(Integer.valueOf(sprid)))._attackSpeed.get(Integer.valueOf(actid))).intValue(); 
    if (actid == 1)
      return 0; 
    return ((Integer)(_dataMap.get(Integer.valueOf(sprid)))._attackSpeed.get(Integer.valueOf(1))).intValue();
  }
  
  public int getMoveSpeed(int sprid, int actid) {
    if (!_dataMap.containsKey(Integer.valueOf(sprid)))
      return 0; 
    if ((_dataMap.get(Integer.valueOf(sprid)))._moveSpeed.containsKey(Integer.valueOf(actid)))
      return ((Integer)(_dataMap.get(Integer.valueOf(sprid)))._moveSpeed.get(Integer.valueOf(actid))).intValue(); 
    if (actid == 0)
      return 0; 
    return ((Integer)(_dataMap.get(Integer.valueOf(sprid)))._moveSpeed.get(Integer.valueOf(0))).intValue();
  }
  
  public int getDirSpellSpeed(int sprid) {
    if (_dataMap.containsKey(Integer.valueOf(sprid)))
      return (_dataMap.get(Integer.valueOf(sprid)))._dirSpellSpeed; 
    return 0;
  }
  
  public int getNodirSpellSpeed(int sprid) {
    if (_dataMap.containsKey(Integer.valueOf(sprid)))
      return (_dataMap.get(Integer.valueOf(sprid)))._nodirSpellSpeed; 
    return 0;
  }
  
  public int getDirSpellSpeed30(int sprid) {
    if (_dataMap.containsKey(Integer.valueOf(sprid)))
      return (_dataMap.get(Integer.valueOf(sprid)))._dirSpellSpeed30; 
    return 0;
  }
  
  public int getDmg(int sprid) {
    if (_dataMap.containsKey(Integer.valueOf(sprid)))
      return (_dataMap.get(Integer.valueOf(sprid)))._dmg; 
    return 0;
  }
  
  public long spr_move_speed(int tempCharGfx) {
    return 200L;
  }
  
  public long spr_attack_speed(int tempCharGfx) {
    return 200L;
  }
  
  public long spr_skill_speed(int tempCharGfx) {
    return 200L;
  }
  
  private static class Spr {
    private final Map<Integer, Integer> _moveSpeed = new HashMap<>();
    
    private final Map<Integer, Integer> _attackSpeed = new HashMap<>();
    
    private int _nodirSpellSpeed = 0;
    
    private int _dirSpellSpeed = 0;
    
    private int _dirSpellSpeed30 = 0;
    
    private int _dmg = 0;
    
    private Spr() {}
  }
}
